using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using TST.SharePoint.Shared;
using System.Xml;
using System.Collections;
using TST.SharePointObjects.ContentWebParts.Shared.WebControls;

namespace TST.SharePointObjects.ContentWebParts
{
    public class GridConfiguration: ConfigurationControl
    {
        protected Table tableGridOptions;
        protected Label labelDisplayOption;
        protected Label labelSelectGrid;
        protected Panel panelFieldOptions;
        protected Table tableFieldOptions;
        protected Label labelItemMenu;
        protected SortDropDownList selectItemMenu;
        protected Label labelGroupBy;
        protected SortDropDownList selectGroupBy;
        protected Label labelSortBy;
        protected SortDropDownList selectSortBy;
        protected SortDropDownList selectSortOrder;
        protected RadioButtonList selectGrid;
        protected TableRow rowItemMenu;
        protected TableRow rowGroupBy;
        protected TableRow rowSortBy;
        protected TableRow rowPageSize;
        protected Label labelPageSize;
        protected TextBox editPageSize;
        protected Label labelPageSizeHelp;
        protected TableRow rowMaxRows;
        protected Label labelMaxItems;
        protected Label labelMaxItemsHelp;
        protected TextBox editMaxItems;
        protected TableRow rowPagerMode;
        protected Label labelPagerMode;
        protected SortDropDownList selectPageMode;
        protected TableRow rowPagerPosition;
        protected Label labelPagerPosition;
        protected SortDropDownList selectPagerPosition;
        protected TableRow rowPageButtonCount;
        protected Label labelPageButtonCount;
        protected TextBox editPageButtonCount;
        protected Label labelPageButtonCountHelp;
        protected TableRow rowStartDate;
        protected Label labelStartDate;
        protected SortDropDownList selectStartDate;
        protected TableRow rowEndDate;
        protected Label labelEndDate;
        protected SortDropDownList selectEndDate;
        protected TableRow rowTitleField;
        protected Label labelTitleField;
        protected SortDropDownList selectTitleField;
        protected TableRow rowLocationField;
        protected Label labelLocationField;
        protected SortDropDownList selectLocationField;
        protected TableRow rowDescriptionField;
        protected Label labelDescriptionField;
        protected SortDropDownList selectDescriptionField;
        protected TableRow rowIsAllDayField;
        protected Label labelIsAllDayField;
        protected SortDropDownList selectIsAllDayField;
        protected TableRow rowOpenInNewWindow;
        protected Label labelOpenInNewWindow;
        protected CheckBox cbkOpenInNewWindow;

        public void WriteConfiguration()
        {
            if (CurrentConfiguration == null || Datasource == null)
            {
                return;
            }

            // Save the group by field and sorting options to the configuration.
            CurrentConfiguration.GroupBy = selectGroupBy.SelectedValue;
            CurrentConfiguration.SortBy = selectSortBy.SelectedValue;
            CurrentConfiguration.SortOrder = selectSortOrder.SelectedValue;
            CurrentConfiguration.ItemMenu = selectItemMenu.SelectedValue;
            CurrentConfiguration.GridType = (DatasourceConfiguration.GridTypes)Enum.Parse(typeof(DatasourceConfiguration.GridTypes), selectGrid.SelectedValue, true);
            CurrentConfiguration.PagerMode = (DatasourceConfiguration.GridPagerMode)Enum.Parse(typeof(DatasourceConfiguration.GridPagerMode), selectPageMode.SelectedValue, true);
            CurrentConfiguration.PagerPosition = (PagerPosition)Enum.Parse(typeof(PagerPosition), selectPagerPosition.SelectedValue, true);
            CurrentConfiguration.StartDateField = selectStartDate.SelectedValue;
            CurrentConfiguration.EndDateField = selectEndDate.SelectedValue;
            CurrentConfiguration.TitleField = selectTitleField.SelectedValue;
            CurrentConfiguration.LocationField = selectLocationField.SelectedValue;
            CurrentConfiguration.DescriptionField = selectDescriptionField.SelectedValue;
            CurrentConfiguration.IsAllDayField = selectIsAllDayField.SelectedValue;
            CurrentConfiguration.OpenAllLinksInNewWindows = cbkOpenInNewWindow.Checked;

            int pageSize = 0;
            if (!string.IsNullOrEmpty(editPageSize.Text) && int.TryParse(editPageSize.Text, out pageSize))
                CurrentConfiguration.PageSize = pageSize;
            else
                CurrentConfiguration.PageSize = 0;

            int maxItems = 0;
            if (!string.IsNullOrEmpty(editMaxItems.Text) && int.TryParse(editMaxItems.Text, out maxItems))
                CurrentConfiguration.MaximumItems = maxItems;
            else
                CurrentConfiguration.MaximumItems = 0;

            int buttonCount = 0;
            if (!string.IsNullOrEmpty(editPageButtonCount.Text) && int.TryParse(editPageButtonCount.Text, out buttonCount))
                CurrentConfiguration.PageButtonCount = buttonCount;
            else
                CurrentConfiguration.PageButtonCount = 0;
        }

        public void ReadConfiguration()
        {
            if (CurrentConfiguration == null || Datasource == null)
            {
                return;
            }

            // Select the correct value of the grouping dropdown.
            selectGroupBy.SelectedValue = CurrentConfiguration.GroupBy;
            selectItemMenu.SelectedValue = CurrentConfiguration.ItemMenu;
            selectSortBy.SelectedValue = CurrentConfiguration.SortBy;
            selectSortOrder.SelectedValue = CurrentConfiguration.SortOrder;
            selectGrid.SelectedValue = CurrentConfiguration.GridType.ToString();
            editPageSize.Text = CurrentConfiguration.PageSize.ToString();
            editMaxItems.Text = CurrentConfiguration.MaximumItems.ToString();
            selectPageMode.SelectedValue = CurrentConfiguration.PagerMode.ToString();
            selectPagerPosition.SelectedValue = CurrentConfiguration.PagerPosition.ToString();
            editPageButtonCount.Text = CurrentConfiguration.PageButtonCount.ToString();
            selectStartDate.SelectedValue = CurrentConfiguration.StartDateField;
            selectEndDate.SelectedValue = CurrentConfiguration.EndDateField;
            selectTitleField.SelectedValue = CurrentConfiguration.TitleField;
            selectLocationField.SelectedValue = CurrentConfiguration.LocationField;
            selectDescriptionField.SelectedValue = CurrentConfiguration.DescriptionField;
            selectIsAllDayField.SelectedValue = CurrentConfiguration.IsAllDayField;
            cbkOpenInNewWindow.Checked = CurrentConfiguration.OpenAllLinksInNewWindows;

        }

        private void AddDataSourceFields()
        {
            string valueItemMenu = selectItemMenu.SelectedValue;
            string valueGroup = selectGroupBy.SelectedValue;
            string valueSortBy = selectSortBy.SelectedValue;
            string valueStartDate = selectStartDate.SelectedValue;
            string valueEndDate = selectEndDate.SelectedValue;
            string valueTitle = selectTitleField.SelectedValue;
            string valueLocation = selectLocationField.SelectedValue;
            string valueDescription = selectDescriptionField.SelectedValue;
            string valueIsAllDayEvent = selectIsAllDayField.SelectedValue;
            // Reset the dropdown to select the grouping field and add an 'empty' item.
            selectItemMenu.Items.Clear();
            selectGroupBy.Items.Clear();
            selectSortBy.Items.Clear();
            selectStartDate.Items.Clear();
            selectEndDate.Items.Clear();
            selectTitleField.Items.Clear();
            selectLocationField.Items.Clear();
            selectDescriptionField.Items.Clear();
            selectIsAllDayField.Items.Clear();

            ListItem emptyItemMenu = new ListItem(string.Format("-- {0} --", TST.SharePointObjects.ContentWebParts.TST_SharePointObjects_ContentWebParts.PleaseSelectAField), Guid.Empty.ToString("B"));
            ListItem emptyGroup = new ListItem(string.Format("-- {0} --", TST.SharePointObjects.ContentWebParts.TST_SharePointObjects_ContentWebParts.PleaseSelectAField), Guid.Empty.ToString("B"));
            ListItem emptySortGroup = new ListItem(string.Format("-- {0} --", TST.SharePointObjects.ContentWebParts.TST_SharePointObjects_ContentWebParts.PleaseSelectAField), Guid.Empty.ToString("B"));
            ListItem emptyStartDateGroup = new ListItem(string.Format("-- {0} --", TST.SharePointObjects.ContentWebParts.TST_SharePointObjects_ContentWebParts.PleaseSelectAField), Guid.Empty.ToString("B"));
            ListItem emptyEndDateGroup = new ListItem(string.Format("-- {0} --", TST.SharePointObjects.ContentWebParts.TST_SharePointObjects_ContentWebParts.PleaseSelectAField), Guid.Empty.ToString("B"));
            ListItem emptyTitleGroup = new ListItem(string.Format("-- {0} --", TST.SharePointObjects.ContentWebParts.TST_SharePointObjects_ContentWebParts.PleaseSelectAField), Guid.Empty.ToString("B"));
            ListItem emptyLocationGroup = new ListItem(string.Format("-- {0} --", TST.SharePointObjects.ContentWebParts.TST_SharePointObjects_ContentWebParts.PleaseSelectAField), Guid.Empty.ToString("B"));
            ListItem emptyDescriptionGroup = new ListItem(string.Format("-- {0} --", TST.SharePointObjects.ContentWebParts.TST_SharePointObjects_ContentWebParts.PleaseSelectAField), Guid.Empty.ToString("B"));
            ListItem emptyIsAllDayGroup = new ListItem(string.Format("-- {0} --", TST.SharePointObjects.ContentWebParts.TST_SharePointObjects_ContentWebParts.PleaseSelectAField), Guid.Empty.ToString("B"));

            selectItemMenu.Items.Add(emptyItemMenu);
            selectGroupBy.Items.Add(emptyGroup);
            selectSortBy.Items.Add(emptySortGroup);
            selectStartDate.Items.Add(emptyStartDateGroup);
            selectEndDate.Items.Add(emptyEndDateGroup);
            selectTitleField.Items.Add(emptyTitleGroup);
            selectLocationField.Items.Add(emptyLocationGroup);
            selectDescriptionField.Items.Add(emptyDescriptionGroup);
            selectIsAllDayField.Items.Add(emptyIsAllDayGroup);

            // Iterate through all fields in the datasource.
            foreach (Field field in Datasource.Fields)
            {
                if (field.FieldType != FieldType.Url)
                {
                    ListItem groupItem = new ListItem(field.DisplayName, field.Name);
                    groupItem.Selected = (string.Compare(field.Id, valueGroup, true) == 0);
                    selectGroupBy.Items.Add(groupItem);

                    ListItem sortItem = new ListItem(field.DisplayName, field.Name);
                    sortItem.Selected = (string.Compare(field.Id, valueSortBy, true) == 0);
                    selectSortBy.Items.Add(sortItem);
                }
                else
                {
                    ListItem itemmenuItem = new ListItem(field.DisplayName, field.Name);
                    itemmenuItem.Selected = (string.Compare(field.Id, valueItemMenu, true) == 0);
                    selectItemMenu.Items.Add(itemmenuItem);
                }
            }
            foreach (FieldReference selected in EditConfiguration.Fields)
            {
                if (Datasource.Fields.Contains(selected.Id))
                {
                    Field selectedField = Datasource.Fields[selected.Id];
                    switch (selectedField.FieldType)
                    {
                        case FieldType.DateTime:
                            ListItem startDateItem = new ListItem(selectedField.DisplayName, selectedField.Id);
                            startDateItem.Selected = (string.Compare(selectedField.Id, valueStartDate, true) == 0);
                            selectStartDate.Items.Add(startDateItem);

                            ListItem endDateItem = new ListItem(selectedField.DisplayName, selectedField.Id);
                            endDateItem.Selected = (string.Compare(selectedField.Id, valueEndDate, true) == 0);
                            selectEndDate.Items.Add(endDateItem);
                            break;
                        case FieldType.File:
                        case FieldType.String:
                            ListItem titleItem = new ListItem(selectedField.DisplayName, selectedField.Id);
                            titleItem.Selected = (string.Compare(selectedField.Id, valueTitle, true) == 0);
                            selectTitleField.Items.Add(titleItem);

                            ListItem locationItem = new ListItem(selectedField.DisplayName, selectedField.Id);
                            locationItem.Selected = (string.Compare(selectedField.Id, valueLocation, true) == 0);
                            selectLocationField.Items.Add(locationItem);

                            ListItem descriptionItem = new ListItem(selectedField.DisplayName, selectedField.Id);
                            descriptionItem.Selected = (string.Compare(selectedField.Id, valueDescription, true) == 0);
                            selectDescriptionField.Items.Add(descriptionItem);                            

                            ListItem isAllDayItem = new ListItem(selectedField.DisplayName, selectedField.Id);
                            isAllDayItem.Selected = (string.Compare(selectedField.Id, valueIsAllDayEvent, true) == 0);
                            selectIsAllDayField.Items.Add(isAllDayItem);

                            break;
                    }
                }
            }
        }

        public void Clear()
        {
            if (Datasource != null && CurrentConfiguration != null)
            {
                CurrentConfiguration.GroupBy = Guid.Empty.ToString("B");
                CurrentConfiguration.SortBy = Guid.Empty.ToString("B");
                CurrentConfiguration.ItemMenu = Guid.Empty.ToString("B");
                CurrentConfiguration.StartDateField = Guid.Empty.ToString("B");
                CurrentConfiguration.EndDateField = Guid.Empty.ToString("B");
                CurrentConfiguration.TitleField = Guid.Empty.ToString("B");
                CurrentConfiguration.LocationField = Guid.Empty.ToString("B");
                CurrentConfiguration.DescriptionField = Guid.Empty.ToString("B");
                CurrentConfiguration.IsAllDayField = Guid.Empty.ToString("B");
                Datasource.ClearFields();
                if (Datasource.Configuration != null)
                    Datasource.Configuration.Fields.Clear();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            selectGrid.SelectedIndexChanged += new EventHandler(selectGrid_SelectedIndexChanged);
            
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            AddDataSourceFields();
            string valueGrid = selectGrid.SelectedValue;
            selectGrid.Items.Clear();
            selectGrid.Items.Add(new ListItem("Grid", DatasourceConfiguration.GridTypes.SharePointGrid.ToString()));
            selectGrid.Items.Add(new ListItem("Calendar", DatasourceConfiguration.GridTypes.SharePointCalendar.ToString()));
            selectGrid.SelectedValue = valueGrid;

            string valuePageMode = selectPageMode.SelectedValue;
            selectPageMode.Items.Clear();
            selectPageMode.Items.Add(new ListItem("Numeric", DatasourceConfiguration.GridPagerMode.NumericButtons.ToString()));
            selectPageMode.Items.Add(new ListItem("Numeric: First/Last", DatasourceConfiguration.GridPagerMode.NumericFirstLastButtons.ToString()));
            selectPageMode.Items.Add(new ListItem("Buttons: Next/Prev", DatasourceConfiguration.GridPagerMode.NextPreviousButtons.ToString()));
            selectPageMode.Items.Add(new ListItem("Buttons: Next/Prev/First/Last", DatasourceConfiguration.GridPagerMode.NextPreviousFirstLastButtons.ToString()));
            selectPageMode.Items.Add(new ListItem("Images: Next/Prev", DatasourceConfiguration.GridPagerMode.NextPreviousImages.ToString()));
            selectPageMode.Items.Add(new ListItem("Images: Next/Prev/First/Last", DatasourceConfiguration.GridPagerMode.NextPreviousFirstLastImages.ToString()));
            selectPageMode.SelectedValue = valuePageMode;

            string valuePagerPosition = selectPagerPosition.SelectedValue;
            selectPagerPosition.Items.Clear();
            selectPagerPosition.Items.Add(new ListItem("Bottom", PagerPosition.Bottom.ToString()));
            selectPagerPosition.Items.Add(new ListItem("Top", PagerPosition.Top.ToString()));
            selectPagerPosition.Items.Add(new ListItem("Top and Bottom", PagerPosition.TopAndBottom.ToString()));
            selectPagerPosition.SelectedValue = valuePagerPosition;

            string valueSortOrder = selectSortOrder.SelectedValue;
            selectSortOrder.Items.Clear();
            selectSortOrder.Items.Add(new ListItem(TST.SharePointObjects.ContentWebParts.TST_SharePointObjects_ContentWebParts.Ascending, "asc"));
            selectSortOrder.Items.Add(new ListItem(TST.SharePointObjects.ContentWebParts.TST_SharePointObjects_ContentWebParts.Descending, "desc"));
            selectSortOrder.SelectedValue = valueSortOrder;

            if (!Page.IsPostBack)
            {
                ReadConfiguration();
            }
            InitDisplayRows();
        }

        private void selectGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            WriteConfiguration();
        }

        private void InitDisplayRows()
        {
            switch (CurrentConfiguration.GridType)
            {
                case DatasourceConfiguration.GridTypes.SharePointCalendar:
                    rowItemMenu.Visible = false;
                    rowGroupBy.Visible = false;
                    rowSortBy.Visible = false;
                    rowPageSize.Visible = false;
                    rowMaxRows.Visible = true;
                    rowPagerMode.Visible = false;
                    rowPagerPosition.Visible = false;
                    rowPageButtonCount.Visible = false;
                    rowStartDate.Visible = true;
                    rowEndDate.Visible = true;
                    rowTitleField.Visible = true;
                    rowLocationField.Visible = true;
                    rowDescriptionField.Visible = true;
                    rowIsAllDayField.Visible = true;
                    rowOpenInNewWindow.Visible = false;
                    break;
                default:
                    rowItemMenu.Visible = true;
                    rowGroupBy.Visible = true;
                    rowSortBy.Visible = true;
                    rowPageSize.Visible = true;
                    rowMaxRows.Visible = true;
                    rowPagerMode.Visible = true;
                    rowPagerPosition.Visible = true;
                    rowPageButtonCount.Visible = true;
                    rowStartDate.Visible = false;
                    rowEndDate.Visible = false;
                    rowTitleField.Visible = false;
                    rowLocationField.Visible = false;
                    rowDescriptionField.Visible = false;
                    rowIsAllDayField.Visible = false;
                    rowOpenInNewWindow.Visible = true;
                    break;
            }
        }

    }
}
